/*
let a = [1, 2, 3, 4, 5];
let b = a.map(x => x * 2);
console.log(b); // [2, 4, 6, 8, 10]

let c = a.map((x) => x * x);
console.log(c); // [1, 4, 9, 16, 25]

let d = a.filter(x => x % 2 === 0);
console.log(d); // [2, 4]

let e = ["f", "7", "d", 1, "8"];
let e1 =e.sort();
console.log(e1); // [1, 2, 5, 7, 8]

let f = [33, 4, 1111, 222];
let f1 = f.sort((a, b) => a - b);
console.log(f1); // [4, 33, 222, 1111]
let f2 = f.sort();
console.log(f2); // [1111, 222, 33, 4]
*/

/*
let count = [1, , 3];
console.log(count.length); // 3
console.log(count[0], count[1], count[2]);

let undefs = [,,,];
console.log(undefs.length); // 3
console.log(undefs[0], undefs[1], undefs[2]); // undefined undefined undefined
*/

/*
let arr1 = ["hello", "world", ,];
let letter1 =arr1.join();
console.log(letter1 + "\n" + typeof letter1); // hello,world,

let letter2 = [...arr1];
console.log(`letter2: ${letter2} | typeof letter2: ${typeof letter2}`); // letter2: hello,world,

let string = "";
for (let letter of arr1) {
	string += letter;
	string += " ";
}
console.log(string);  		// hello world undefined

for (let [index, letter] of arr1.entries()) {
	console.log(`${index}: ${letter}`);
}

let uppercase = "";
arr1.forEach(letter => {
	uppercase += letter.toUpperCase();
	uppercase += " ";
});
console.log(uppercase); // HELLO WORLD
*/

/*
let arr = [1, 2, 3, 4, 5], sum = 0;

// 计算数组元素的和
arr.forEach(value => { sum += value; });
console.log(sum);				// sum = 15

console.log(`before increment: ${arr}`); // before increment: 1,2,3,4,5
// 递增每个元素的值
arr.forEach((value, index, array) => { array[index] = value + 1; });
console.log(`after increment: ${arr}`); // after increment: 2,3,4,5,6
*/

// let a = [1, 2, 3, 4, 5];
// let b = a.map(x => x * 2);
// console.log(b);

// let a = [1, 2, 3, 4, 5];
// let b = a.find(x => x % 2 == 0); // 2
// let c = a.findIndex(x => x % 2 == 0);		// 1
// console.log(b, c);		// 

let arr_m = [-2, -1, 0, 1, 2];
let arr_p = arr_m.flatMap(x => [x, x * 2])
console.log(arr_p);

let arr_n = arr_m.flatMap(x => x<0?[]:Math.sqrt(x))
console.log(arr_n);